Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fp-ts

Package Overview
Dependencies
Maintainers
1
Versions
231
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fp-ts

Functional programming in TypeScript

  • 0.4.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.6M
increased by9.05%
Maintainers
1
Weekly downloads
 
Created

What is fp-ts?

The fp-ts npm package is a library for functional programming in TypeScript. It provides developers with tools to write code in a functional style, leveraging concepts like type-safe functional combinators, monads, and other abstractions that enable expressive and concise code.

What are fp-ts's main functionalities?

Option Type

The Option type represents encapsulation of an optional value. A value of type Option<T> can either be some<T> if it exists or none if it does not. This is useful for handling cases where a value might be missing without resorting to null or undefined.

import { Option, some, none } from 'fp-ts/Option';

function find<T>(predicate: (a: T) => boolean, arr: T[]): Option<T> {
  for (const item of arr) {
    if (predicate(item)) {
      return some(item);
    }
  }
  return none;
}

const result = find(x => x > 10, [1, 2, 3]);
console.log(result); // output: none

Either Type

The Either type represents a value of one of two possible types (a disjoint union). An instance of Either is either an instance of left or right. It is useful for error handling where left can be used to hold an error value and right can hold a success value.

import { Either, left, right } from 'fp-ts/Either';

function divide(a: number, b: number): Either<string, number> {
  return b === 0 ? left('Cannot divide by zero') : right(a / b);
}

const result = divide(10, 0);
console.log(result); // output: left('Cannot divide by zero')

IO Type

The IO type represents a computation that can perform side effects when executed. It is a way to manage side effects in a functional way by deferring their execution.

import { IO } from 'fp-ts/IO';

const log: IO<void> = () => console.log('Hello, fp-ts!');

log(); // output: 'Hello, fp-ts!'

Function Composition

Function composition is a core concept in functional programming, allowing you to combine multiple functions into a single function. The flow function from fp-ts helps you to compose functions easily.

import { flow } from 'fp-ts/function';

const toUpperCase = (s: string) => s.toUpperCase();
const exclaim = (s: string) => `${s}!`;
const shout = flow(toUpperCase, exclaim);

console.log(shout('hello')); // output: 'HELLO!'

Other packages similar to fp-ts

Keywords

FAQs

Package last updated on 04 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc